/* 

Date: 04/03/2024

This file contains the code used for the main analyses (Tables 2-5) reported in Vladasel et al. 2024, Revenue Drift,
Incentives, and Effort Allocation in Social Enterprises, Journal of Economics & Management Strategy, forthcoming. The
sample in the datafile already applies all restrictions discussed in the paper; all variables are described in the paper.
For queries related to the analysis, please email Theodor Vladasel at theodor.vladasel@upf.edu.

*/


** Load data

clear
cd "..." // insert relevant directory here
use vpsvp_jems_2024.dta
set matsize 11000
set more off

** Composite measure of social preferences

factor compassion dictator ultimatum lottery sharewill prosocial
rotate
matrix A = e(r_Ev)
di A[1,1]/(A[1,1]+A[1,2]+A[1,3])
estat factors
predict factor1
rename factor1 socmotiv

** Social effort share

foreach var of newlist C_FP C_NP C_SE T_FP T_NP T_SE {
	gen `var'_balance = `var'_soc/`var'_tot
}


** Table 2: Descriptive Statistics
sum female student compassion dictator ultimatum lottery sharewill np_employ se_employ volunteer ///
	donate workse prosocial socmotiv riskwill timewill time_practice time_comprehension time_questions time_all
tab age
tab education
tab income


** Table 3: Social Effort, by Contract and Treatment

	** Panel A, columns 1-4
	
tabstat C_FP_soc, by(treatment) stat(mean sd N)
tabstat C_NP_soc, by(treatment) stat(mean sd N)
tabstat C_SE_soc, by(treatment) stat(mean sd N)
	
	** Panel A, columns 5-8
	
tabstat T_FP_soc if choice == 1, by(treatment) stat(mean sd N)
tabstat T_NP_soc if choice == 2, by(treatment) stat(mean sd N)
tabstat T_SE_soc if choice == 3, by(treatment) stat(mean sd N)

	** Panel D, columns 1-4
	
gen C_avg_soc = (C_FP_soc + C_NP_soc)/2
ttest C_SE_soc = C_avg_soc if treatment == 1
ttest C_SE_soc = C_avg_soc if treatment == 2
ttest C_SE_soc = C_avg_soc if treatment == 3
ttest C_SE_soc = C_avg_soc if treatment == 4
drop C_avg*

	** Panel D, columns 5-8
	
gen VT_soc = T_FP_soc if choice == 1
replace VT_soc = T_NP_soc if choice == 2
replace VT_soc = T_SE_soc if choice == 3
forvalues i = 1(1)4 {
	reg VT_soc choose_FP choose_NP if treatment == `i', robust
	test choose_FP + choose_NP = 0
}
drop VT_*

	** Panel C, columns 1-4
	
		** FP vs SE
ttest C_FP_soc = C_SE_soc if treatment == 1 
ttest C_FP_soc = C_SE_soc if treatment == 2
ttest C_FP_soc = C_SE_soc if treatment == 3
ttest C_FP_soc = C_SE_soc if treatment == 4

		** NP vs SE
ttest C_NP_soc = C_SE_soc if treatment == 1
ttest C_NP_soc = C_SE_soc if treatment == 2
ttest C_NP_soc = C_SE_soc if treatment == 3
ttest C_NP_soc = C_SE_soc if treatment == 4


/* Note: the following reshape is necessary to compare effort under sorting. */

***** SOCIAL EFFORT LEVELS BY TREATMENT AND SORTING *****

foreach var of newlist com soc tot {
	replace T_FP_`var' = . if choose_FP == 0
	replace T_NP_`var' = . if choose_NP == 0
	replace T_SE_`var' = . if choose_SE == 0
}

***** RE-SHAPE DATA LONG TO COMPARE UNDER SORTING *****

rename C_FP_soc C_soc1
rename C_NP_soc C_soc2
rename C_SE_soc C_soc3
rename T_FP_soc T_soc1
rename T_NP_soc T_soc2
rename T_SE_soc T_soc3
rename C_FP_com C_com1
rename C_NP_com C_com2
rename C_SE_com C_com3 
rename T_FP_com T_com1
rename T_NP_com T_com2
rename T_SE_com T_com3
rename C_FP_tot C_tot1
rename C_NP_tot C_tot2
rename C_SE_tot C_tot3
rename T_FP_tot T_tot1
rename T_NP_tot T_tot2
rename T_SE_tot T_tot3
rename C_FP_balance C_balance1
rename C_NP_balance C_balance2
rename C_SE_balance C_balance3
rename T_FP_balance T_balance1
rename T_NP_balance T_balance2
rename T_SE_balance T_balance3

reshape long C_soc T_soc C_com T_com C_tot T_tot C_balance T_balance, i(subject_ID) j(contract)
gen contract2 = "FP" if contract == 1
replace contract2 = "NP" if contract == 2
replace contract2 = "SE" if contract == 3
drop contract
rename contract2 contract
egen pick_ind = tag(subject_ID)


	** Panel C, columns 5-8
	
		** FP vs SE
ttest T_soc if contract != "NP" & treatment == 1, by(contract)
ttest T_soc if contract != "NP" & treatment == 2, by(contract)
ttest T_soc if contract != "NP" & treatment == 3, by(contract)
ttest T_soc if contract != "NP" & treatment == 4, by(contract)

		** SE vs NP
ttest T_soc if contract != "FP" & treatment == 1, by(contract)
ttest T_soc if contract != "FP" & treatment == 2, by(contract)
ttest T_soc if contract != "FP" & treatment == 3, by(contract)
ttest T_soc if contract != "FP" & treatment == 4, by(contract)
	
	** Panel B, columns 1-4
	
ttest C_soc if contract == "SE" & (treatment == 1 | treatment == 2), by(treatment)
ttest C_soc if contract == "SE" & (treatment == 1 | treatment == 3), by(treatment)
ttest C_soc if contract == "SE" & (treatment == 1 | treatment == 4), by(treatment)
ttest C_soc if contract == "SE" & (treatment == 2 | treatment == 3), by(treatment)
ttest C_soc if contract == "SE" & (treatment == 2 | treatment == 4), by(treatment)
ttest C_soc if contract == "SE" & (treatment == 3 | treatment == 4), by(treatment)
	
	** Panel B, columns 5-8
	
ttest T_soc if contract == "SE" & (treatment == 1 | treatment == 2), by(treatment)
ttest T_soc if contract == "SE" & (treatment == 1 | treatment == 3), by(treatment)
ttest T_soc if contract == "SE" & (treatment == 1 | treatment == 4), by(treatment)
ttest T_soc if contract == "SE" & (treatment == 2 | treatment == 3), by(treatment)
ttest T_soc if contract == "SE" & (treatment == 2 | treatment == 4), by(treatment)
ttest T_soc if contract == "SE" & (treatment == 3 | treatment == 4), by(treatment)
	

** Table 4: Motivation and Contract Choice

global controls female student i.education i.background i.income i.age i.mission
sum compassion if pick_ind, d
gen z_com = (compassion - r(mean))/r(sd)
sum z_com, d

	** Panel A, columns 1-4
	
tabstat z_com socmotiv if pick_ind, by(treatment) stat(mean sd n)

	** Panel A, columns 5-8
	
tabstat socmotiv socmotiv if pick_ind, by(treatment) stat(mean sd n)

	** Panel B, columns 1-4
	
tabstat z_com if pick_ind & choice == 1, by(treatment) stat(mean sd n)
tabstat z_com if pick_ind & choice == 3, by(treatment) stat(mean sd n)
tabstat z_com if pick_ind & choice == 2, by(treatment) stat(mean sd n)

	** Panel B, columns 5-8
	
tabstat socmotiv if pick_ind & choice == 1, by(treatment) stat(mean sd n)
tabstat socmotiv if pick_ind & choice == 3, by(treatment) stat(mean sd n)
tabstat socmotiv if pick_ind & choice == 2, by(treatment) stat(mean sd n)

	** Panel C, columns 1-4

preserve
	keep if pick_ind & choice == 3
	ttest z_com if (treatment == 1 | treatment == 2), by(treatment)
	ttest z_com if (treatment == 1 | treatment == 3), by(treatment)
	ttest z_com if (treatment == 1 | treatment == 4), by(treatment)
	ttest z_com if (treatment == 2 | treatment == 3), by(treatment)
	ttest z_com if (treatment == 2 | treatment == 4), by(treatment)
	ttest z_com if (treatment == 3 | treatment == 4), by(treatment)
restore

	** Panel C, columns 5-8

preserve
	keep if pick_ind & choice == 3
	ttest socmotiv if (treatment == 1 | treatment == 2), by(treatment)
	ttest socmotiv if (treatment == 1 | treatment == 3), by(treatment)
	ttest socmotiv if (treatment == 1 | treatment == 4), by(treatment)
	ttest socmotiv if (treatment == 2 | treatment == 3), by(treatment)
	ttest socmotiv if (treatment == 2 | treatment == 4), by(treatment)
	ttest socmotiv if (treatment == 3 | treatment == 4), by(treatment)
restore

	** Panel D, columns 1-8
	
		** FP vs SE
		
preserve
	keep if pick_ind & choice != 2
	ttest z_com if treatment == 1, by(choice)
	ttest z_com if treatment == 2, by(choice)
	ttest z_com if treatment == 3, by(choice)
	ttest z_com if treatment == 4, by(choice)
	ttest socmotiv if treatment == 1, by(choice)
	ttest socmotiv if treatment == 2, by(choice)
	ttest socmotiv if treatment == 3, by(choice)
	ttest socmotiv if treatment == 4, by(choice)
restore

		** SE vs NP

preserve
	keep if pick_ind & choice != 1
	ttest z_com if treatment == 1, by(choice)
	ttest z_com if treatment == 2, by(choice)
	ttest z_com if treatment == 3, by(choice)
	ttest z_com if treatment == 4, by(choice)
	ttest socmotiv if treatment == 1, by(choice)
	ttest socmotiv if treatment == 2, by(choice)
	ttest socmotiv if treatment == 3, by(choice)
	ttest socmotiv if treatment == 4, by(choice)
restore

	** Panel E, columns 1-8

eststo clear
foreach var of varlist z_com socmotiv {
	forvalues i = 1(1)4 {
		set more off
		sum `var' if choose_SE & pick_ind & treatment == `i'
		eststo: qui reg `var' choose_FP choose_NP i.mission if pick_ind & treatment == `i', robust
		test choose_FP + choose_NP = 0
	}
}
esttab, replace cells(b(star fmt(3)) se(par fmt(3)) p(par fmt(3))) r2(3) star(* 0.10 ** 0.05 *** 0.01) ///
	mtitles("Comp 0" "Comp .25" "Comp .5" "Comp 1" "Socmv 0" "Socmv .25" "Socmv .5" "Socmv 1") ///
	keep(choose_FP choose_NP)
eststo clear 	


/* Note: the following reshape is necessary to compare intensive and extensive margins. */

***** RESHAPE FOR COMPARING INTENSIVE VS EXTENSIVE MARGIN *****

** Absolute balance
gen C_diff = abs(C_soc - C_com)
gen T_diff = abs(T_soc - T_com)

keep if contract == "SE"
drop pick_ind
rename C_soc social1
rename T_soc social2
rename C_balance balance1
rename T_balance balance2
rename C_diff difference1
rename T_diff difference2

reshape long social balance difference, i(subject_ID) j(sorting)
replace sorting = sorting - 1
egen pick_ind = tag(subject_ID)
drop if sorting & choice != 3


** Table 5: Incentive and Selection Channel Effects

foreach var of varlist social balance difference {
	sum `var' if treatment == 1 & !sorting
	di "se is: " r(sd)/sqrt(r(N))
	
	eststo: qui reg `var' i.treatment##i.sorting, vce(cluster subject_ID)
	** total sorting effects
	lincom 1.sorting + 2.treatment#1.sorting
	lincom 1.sorting + 3.treatment#1.sorting
	lincom 1.sorting + 4.treatment#1.sorting
	
	eststo: qui reg `var' i.treatment##i.sorting $controls, vce(cluster subject_ID)
	** total sorting effects
	lincom 1.sorting + 2.treatment#1.sorting
	lincom 1.sorting + 3.treatment#1.sorting
	lincom 1.sorting + 4.treatment#1.sorting
}
esttab, replace cells(b(star fmt(3)) se(par fmt(3)) p(fmt(3))) r2(3) star(* 0.10 ** 0.05 *** 0.01) ///
	mtitles("Units" "Units" "Share" "Share" "Diff" "Diff") keep(1.sorting 2.treatment 2.treatment*1.sorting /// 
	3.treatment 3.treatment*1.sorting 4.treatment 4.treatment*1.sorting _cons)
eststo clear

